<template>
	<jk-dialog
		v-model="showDialog"
		:title="`选择-${title}`"
		width="1000px"
		append-to-body
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-4 flex-end-center">
			<!--<jk-search-input v-model="searchParams.typeNameOrCode" class="search-bar-item-size2 item-gap" placeholder="存货类型" @on-enter="onSearchBtn"></jk-search-input>-->
			<!--<jk-select v-model="searchParams.processId" :option-list="processList" size="small" placeholder="工序" class="search-bar-item-size1 item-gap"></jk-select>-->
			<jk-search-input v-model="searchParams.empName" class="search-bar-item-size3 item-gap" placeholder="名称" @on-enter="onSearchBtn"></jk-search-input>
			<jk-search-button @click="onSearchBtn"></jk-search-button>
		</header>

		<jk-table
			ref="jkTable"
			:radio-config="{trigger: 'row'}"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			highlight-current-row
			:height="dialogTableHeight - 30"
			@radio-change="onRadioChange"
			@cell-dblclick="onCellDblclick"
		>
			<vxe-table-column type="radio" width="60" align="center" title="单选" fixed="left"></vxe-table-column>
			<vxe-table-column field="empName" title="名称" :width="100"></vxe-table-column>
			<vxe-table-column field="dutyState" title="责任类别" :width="80">
				<template slot-scope="scope">
					<div class="flex-start-center-wrap">
						{{ scope.row.dutyState === 0 ? '保养' : '巡检' }}
					</div>
				</template>
			</vxe-table-column>
			<vxe-table-column field="machineList" title="责任机台" :min-width="120">
				<template slot-scope="scope">
					<div class="flex-start-center-wrap">
						{{ scope.row.machineList ? scope.row.machineList.map(x => x.machineName).join('、') : '' }}
					</div>
				</template>
			</vxe-table-column>
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				layout="total, prev, pager, next"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '@/mixins/cal-dialog-height';
    import { createTree, pageConfig } from '@/utils/jk/jk-tool';
    import { spinProductCategoryList } from '@/api/material/category';
    import { systemBusinessDictPage } from '@/api/basicData/businessDict';
    import { spinTransportUnitPage } from '/src/api/basicData/supplier';
    import { machineManagerUpkeepPersonnelPage } from '../../../../../api/machine/upkeepEmp';

    export default {
        mixins: [calDialogHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            title: {
                type: String,
                default: '名单'
            },
            formData: {
                type: Object,
                default() {
                    return {};
                }
            },
            defaultCategory: { // 默认的物料分类
                type: String,
                default: ''
            },
            defaultStockType: { // 默认的存货类型
                type: String,
                default: ''
            }
        },
        data() {
            return {
                processList: [],
                categoryIds: [],
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    empName: ''
                },
                typeList: [],
                treeData: [],
                selectRow: null,
                searchBarProcessId: null,
                searchBarNameOrCode: '',
                tableData: [],
                showDialog: false,
                tableLoading: false,
                selectData: []
            };
        },
        methods: {
            onCellDblclick(e) {
                this.$emit('on-confirm', e.row);
            },
            onRadioChange(selection) {
                this.selectData = selection.row;
            },
            checkboxMethod({ row }) {
                return row._disabled !== true;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onConfirmBtn() {
                if (Object.keys(this.selectData).length) {
                    this.$emit('on-confirm', this.selectData);
                } else {
                    this.$tipsBar(this, 'unChoose');
                }
            },
            getListRequest() {
                const params = JSON.parse(JSON.stringify(this.searchParams));
                params.dutyState = this.formData.dutyState;
                params.isAlternate = false;
                this.tableLoading = true;
                this.selectData = [];
                return machineManagerUpkeepPersonnelPage(params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            getTreeListRequest() {
                return spinProductCategoryList().then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res ? JSON.parse(JSON.stringify(res.data.res)) : [];
                        this.treeData = createTree(resData, {});
                    }
                });
            },
            getStockTypeListRequest() {
                return systemBusinessDictPage({ categoryCode: 'STOCK_TYPE' }).then(res => {
                    if (res.data.status === 200) {
                        this.typeList = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getStockTypeListRequest();
                await this.getListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
